/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.worsik.android.test.rideshare.util.listener;

import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import cz.worsik.android.test.rideshare.MainActivity;
import cz.worsik.android.test.rideshare.constants.ViewConst;
import cz.worsik.android.test.rideshare.view.KalendarButton;

public class ActivitySwipeDetector implements View.OnTouchListener 
{
	static final Boolean result = false;
	static final String logTag = "ActivitySwipeDetector";
	private MainActivity activity;
	static final int MIN_DISTANCE = 100;
	private float downX, downY, upX, upY;

	public ActivitySwipeDetector(MainActivity activity){
		this.activity = activity;
	}

	public void onRightToLeftSwipe(){
		if (ViewConst.DEBUG) {
			Log.i(logTag, "RightToLeftSwipe!");
		}
	}

	public void onLeftToRightSwipe(){
		if (ViewConst.DEBUG) {
			Log.i(logTag, "LeftToRightSwipe!");
		}
	}

	public void onTopToBottomSwipe(){
		if (ViewConst.DEBUG) {
			Log.i(logTag, "onTopToBottomSwipe!");
		}
		activity.zHoraDolu();
	}

	public void onBottomToTopSwipe(){
		if (ViewConst.DEBUG) {
			Log.i(logTag, "onBottomToTopSwipe!");
		}
		activity.zDolaNahoru();
	}

	public boolean onTouch(View v, MotionEvent event) {
		
		KalendarButton btn = (KalendarButton) v;
		
		switch(event.getAction()){
			case MotionEvent.ACTION_DOWN: {
				downX = event.getX();
				downY = event.getY();
				return true;
			}
			case MotionEvent.ACTION_UP: {
				upX = event.getX();
				upY = event.getY();

				float deltaX = downX - upX;
				float deltaY = downY - upY;

				// swipe horizontal?
				if(Math.abs(deltaX) > MIN_DISTANCE){
					// left or right
					if(deltaX < 0) { this.onLeftToRightSwipe(); return true; }
					if(deltaX > 0) { this.onRightToLeftSwipe(); return true; }
				}
				else {
						activity.pouzeClick(v);
						return true; // We don't consume the event - we do
				}

				// swipe vertical?
				if(Math.abs(deltaY) > MIN_DISTANCE){
					// top or down
					if(deltaY < 0) { this.onTopToBottomSwipe(); return result; }
					if(deltaY > 0) { this.onBottomToTopSwipe(); return result; }
				}
				else {
						activity.pouzeClick(v);
						return true; // We don't consume the event - we do
				}

				return true;
			}
		}
		return false;
	}

}
